/*
 * Language.cpp
 *
 *  Created on: 24.05.2012
 *      Author: Caffeine
 */

#include <irrlicht.h>
#include "Language.h"
#include <irrlicht.h>
#include <StringUtils.h>
#include "log/Log.h"
#ifndef AS_STRING
#define AS_STRING irr::core::stringw
#endif
irr::core::array<AS_STRING> list;

bool readline(irr::io::IReadFile*f,irr::core::stringw*strw){
	char  ch;
	irr::core::stringc str = "";
	while ( 0 != f->read( &ch, 1 ) ){
		if ( ch == '\n'){
			*strw=irr::str::UTF8_to_WChar(str.c_str());
			return true;
		}else
			str += ch;
	}
	return false;
	/*
	for (irr::u32 i=0;i<str.size();i+=2){
		wchar_t a;
		char*b1=(char*)&a;
		b1[0]=str[i*2];
		b1[1]=str[i*2+1];
		(*strw)+=a;
	}
	strw=str.c_str();
	return false;
	*/
}

bool readtoken(irr::io::IReadFile*f,irr::core::stringw*strw)
{
  char  ch;
  irr::core::stringc str = "";
  while ( 0 != f->read( &ch, 1 ) )
  {
    if ( ( ch == ' ') || ( ch == '\t') ){
    	*strw=str;
      return true;
    }else
      str += ch;
  }
  *strw=str.c_str();
  return false;
}

AS_STRING Language::getString(irr::u16 index){
	if (index>=list.size())
		return "N/A";
	return list[index];
}

void Language::loadLanguage(irr::core::stringw path,irr::io::IFileSystem*fs){
	irr::io::IReadFile*file=fs->createAndOpenFile(path);
	AS_STRING line;
	while (readline(file,&line)){
		AS_STRING t=line;
		list.push_back(t);
		line="";
	}
	file->drop();
}

void Language::regAS(){

}
